<!DOCTYPE HTML>
<html>
<head>
<title>global HTML page</title>
<script type="text/javascript">

var egs=localStorage.getItem("searchEngines");
if(egs==null) {
	egs=['baidu','iciba','google','wiki'];
	localStorage.setItem("searchEngines", egs);
} else if(egs=='') egs=new Array();
else egs=egs.split(",");

var zoomLevel=localStorage.getItem("zoomLevel");
if(zoomLevel==null) {
	zoomLevel=1;
	localStorage.setItem("zoomLevel", zoomLevel);
}

var openTab = {
	rightTab: function () {return indexOfCurrentTab()+1;},
	leftTab: function () {return indexOfCurrentTab();},
	rightMost:function() {return 1024},
	leftMost: function() {return 0} 
};

var tabPos=localStorage.getItem("tabPos");
if(tabPos==null) {
	tabPos=openTab.rightTab;
	localStorage.setItem("tabPos", "rightTab");
} else tabPos=openTab[tabPos];

var activeTab=localStorage.getItem("activeTab");
if(activeTab==null) {
	activeTab="background";
	localStorage.setItem("activeTab", "background");
}

var procFunc = {
	request_init_args: 
		function(e) {
			var tab = e.target;
			tab.page.dispatchMessage("init_args", {Engines:egs, zoomLevel:zoomLevel});
		},
	search:
		function(e) {
			var msg = e.message;
			var id = msg.id;
			var query = msg.query;
			var url = searchUrls[id].replace(/myquery/,query);
			openUrl(url);
	},
	open_url:
		function(e) {
			var url = e.message;
			openUrl(url);
	},
	update_setting:
		function(e) {
			egs = e.message.Engines;
			zoomLevel = e.message.zoomLevel;
			reloadSetting();
	},
	preview:
		function(e) {
			var tab = e.target;
			tab.page.dispatchMessage("init_args", {Engines:e.message, zoomLevel:null});
	},
	tabPos_change:
		function(e) {
			tabPos=openTab[e.message];
			localStorage.setItem("tabPos", e.message);
	},
	activeTab_change:
		function(e) {
			activeTab=e.message;
			localStorage.setItem("activeTab", activeTab);
	},
	zoom_change:
		function(e) {
			var tab = e.target;
			tab.page.dispatchMessage("zoom_change", e.message);
	},
	load_js:
		function(e) {
			var tab = e.target;
			var src=safari.extension.baseURI+e.message.file;
   	 		var xhr = new XMLHttpRequest();
    		xhr.open("GET", src, false);
    		xhr.send();
			tab.page.dispatchMessage("js_file", {token:e.message.token, file:xhr.responseText});
		},
	frame_dragstart:
		function(e) {
			var tab = e.target;
			var msg = e.message;
			tab.page.dispatchMessage("frame_dragstart", {href:msg.href, sel:msg.sel, event:msg.event});
		},
	frame_dragend:
		function(e) {
			var tab = e.target;
			tab.page.dispatchMessage("frame_dragend", "");
		}
};

safari.application.addEventListener("message",function(mEvent) {
	var process=mEvent.name;
	if(procFunc[process])
		procFunc[process](mEvent);
},false);

safari.application.addEventListener("command", function(commandEvent) {
	if (commandEvent.command == "CoolSearchOptions") {
		safari.application.activeBrowserWindow.openTab("foreground").url = safari.extension.baseURI+"Options.html";
	}
}, false);

function openUrl(url) {
	safari.application.activeBrowserWindow.openTab(activeTab, tabPos()).url = url;
}

function reloadSetting() {
	var bws = safari.application.browserWindows;
	for (var i=0; i<bws.length; ++i) {
		var tabs = bws[i].tabs;
		for (var j=0; j<tabs.length; ++j) {
			if(tabs[j].page!=undefined) 
				tabs[j].page.dispatchMessage("init_args", {Engines:egs, zoomLevel:zoomLevel});
		}
	}
}

function indexOfCurrentTab() {
    var currentTab = safari.application.activeBrowserWindow.activeTab;
    var tabs = safari.application.activeBrowserWindow.tabs;
    var index = 0;

    while (currentTab != tabs[index]) {
        ++index;
    }

    return index;
}

var searchUrls={
    google: "http://www.google.com/search?q=myquery&ie=utf-8&oe=utf-8",
    baidu:  "http://www.baidu.com/s?wd=myquery&ie=utf-8",
    bing:   "http://www.bing.com/search?q=myquery",
    yahoo:  "http://search.yahoo.com/search?p=myquery&ei=UTF-8",
    sogou:  "http://www.sogou.com/web?query=myquery&ie=utf-8",
    emule:  "http://www.verycd.com/search/folders/myquery",
    taobao: "http://search.taobao.com/search?q=myquery&ie=utf-8",
    youku:  "http://www.soku.com/search_video/q_myquery",
    tudou:  "http://so.tudou.com/isearch/myquery",
    iciba:  "http://www.iciba.com/myquery",
    dictcn: "http://dict.cn/myquery",
    wiki:   "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search=myquery",
    macupdate: "http://search.macupdate.com/search.php?keywords=myquery",
    vtrack: "http://www.versiontracker.com/php/qs.php?mode=basic&action=search&str=myquery",
    douban: "http://www.douban.com/search?q=myquery",
    about:  "http://search.about.com/fullsearch.htm?terms=myquery",
    amazon: "http://www.amazon.com/gp/search?ie=UTF8&keywords=myquery&index=blended&linkCode=ur2&camp=1789&cr",
    ask:    "http://www.ask.com/web?q=myquery",
    cnet:   "http://www.cnet.com/4244-5_1-0.html?query=myquery&tag=srch",
    ebay:   "http://search.ebay.com/search/search.dll?satitle=myquery",
    espn:   "http://sports.espn.go.com/keyword/search?searchString=myquery&page=espn",
    fb:     "http://www.facebook.com/s.php?q=myquery&init=q",
    imdb:   "http://www.imdb.com/Find?s=all&q=myquery",
    itunes: "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?songTerm=myquery",
    mtv:    "http://www.mtv.com/search/?searchterm=myquery&searchtype=1"
//    youtube: ""
};

</script>
</head>
<body>
</body>
</html>
